<?php

namespace App\Http\Controllers\Site\Custom\Provider;
use App\B2BProject\Site\HelperControllerBuildTrait;
use App\B2BProject\Custom\Business\PriceStrategy;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;

/**
 * 供应商-价格策略
 * Class PriceStrategyController
 * @package App\Http\Controllers\Site\Custom\Provider
 */
class PriceStrategyController extends Controller
{
    use HelperControllerBuildTrait;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        if ($request->ajax()) {
            $member = Auth::user();
            if (!$request->pageSize) {
                $request->pageSize = 10;
            }
            $request->query->set('page', $request->query('page', $request->page));
            $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));
            $price_strategies = PriceStrategy::where('store_id',
                $member->store->id)->paginate($request->query('pageSize'));

            foreach ($price_strategies as $price_strategy) {
                $data[] = [
                    'id'         => $price_strategy->id,
                    'policyname' => $price_strategy->name,
                    'context'    => $price_strategy->price_strategies,
                    'operate'    => 1
                ];
            }
            $responseText['data']           = $data;
            $responseText['pagingConfData'] = [
                'page'    => $price_strategies->currentPage(),
                'perpage' => $request->query('pageSize'),
                'total'   => $price_strategies->total()
            ];
            return response()->json($responseText);
        }
        return view('host/custom/provider/price-strategy/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function postIndex(Request $request)
    {
        if ($request->ajax()) {
            $v = Validator::make($request->all(), [
                'name'  => 'required',
                'price' => 'required|numeric',
            ]);

            if ($v->fails()) {
                return $v->errors();
            }

            $store = Auth::user()->store; // member表关联store表

            $data['name']             = $request->name;
            $data['price_strategies'] = $request->price;

            $price_strategy = PriceStrategy::create($data);
            $store->priceStrategy()->save($price_strategy); // 保存数据
            return $this->getIndex($request);
        }


    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $member               = Auth::user();
        $price_strategy       = PriceStrategy::where('store_id',
            $member->store->id)->whereId($request->id)->firstOrfail();
        $data                 = [
            'id'         => $price_strategy->id,
            'policyname' => $price_strategy->name,
            'context'    => $price_strategy->price_strategies,
        ];
        $responseText['data'] = $data;
        return response()->json($responseText);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function putIndex(Request $request)
    {
        if ($request->ajax()) {

            $v = Validator::make($request->all(), [
                'id'    => 'required|numeric',
                'name'  => 'required',
                'price' => 'required|numeric',
            ]);

            if ($v->fails()) {
                return $v->errors();
            }

            $store = Auth::user()->store; // member表关联store表

            $data['name']             = $request->name;
            $data['price_strategies'] = $request->price;


            $price_strategy = PriceStrategy::findOrFail($request->id);
            $price_strategy->update($data);

            $store->priceStrategy()->save($price_strategy); // 保存数据
        }
        return $this->getIndex($request);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        if ($request->ajax()) {
            $price_strategy = PriceStrategy::findOrFail($request->id);
            $price_strategy->delete();
        }
        return $this->getIndex($request);
    }
}
